package com.zds.hj.poseidon.biz.dispatcher.credit.demo;

import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.boot.dispatch.dto.ModuleBaseInfo;

import com.zds.hj.icmc.facade.dto.common.applyinput.CreditApplyInputDto;
import com.zds.hj.icmc.facade.dto.common.applyinput.resp.ApplyInputRespDto;
import com.zds.hj.icmc.facade.enums.OrderLifeEnum;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;

import com.zds.hj.poseidon.biz.common.IcmcServiceInterface;
import com.zds.hj.poseidon.biz.dispatcher.credit.CreditCheckAbsHandler;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.poseidon.facade.dto.hpp.credit.enums.OrderStateEnum;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.util.automaton.DaciukMihovAutomatonBuilder;


import javax.annotation.Resource;

/**
 * @author ZY
 */
@DispatcherHandler(value = "CreditSubmitHandler", desc = "融资授信进件网贷中心", applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class CreditSubmitHandler extends CreditCheckAbsHandler {

    private static final int sequenceTog = 12120;



    @Resource
    private IcmcServiceInterface icmcServiceInterface;



    @Resource
    private CreditApplyService creditApplyService;


    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {

        //获取授信申请扩展信息
        try {
            CreditApplyInputDto creditApplyInputDto =new CreditApplyInputDto();
            creditApplyInputDto.setProductNo(dbData.getProductNo());
            //测试数据
            dbData.setEntLegalId(4l);
            dbData.setEnterpriseOrgId(5l);
            dbData.setChannelApplyNo(dbData.getApplyNo());
            dbData.setBusinessLicenseObsId(dbData.getFileId());
            creditApplyInputDto.setCreditApplyReqDto(JSONUtil.toJson(dbData));
            creditApplyInputDto.setRequestNo(dbData.getApplyNo());
            //进件操作
            ApplyInputRespDto respDto = icmcServiceInterface.ylrInputCreditApply(creditApplyInputDto);
            log.info("网贷中心进件成功：{}",respDto);
            dbData.setOrderNo(respDto.getCreditApplyNo());
            dbData.setOrderStateEnum(OrderStateEnum.PROGRESS_CRAFT);
//            dbData.setGlobalStatusCode(OrderLifeEnum.CREDIT_ACCEPTANCE_ING.getCode());
            //查询h5认证信息作为判断
         } catch (Exception e) {
            log.info("申请进件发生异常", e);
            if (e instanceof BusinessException) {
                throw new BusinessException(e.getMessage(), ((BusinessException) e).getCode());
            }
            e.printStackTrace();
            throw new BusinessException("提交失败，请联系管理员");
        }
    }


    /**
     * 设置为节点(不可逆，同步，自动节点)
     *
     * @return ModuleBaseInfo
     */
    @Override
    public ModuleBaseInfo moduleBaseInfo() {
        ModuleBaseInfo moduleBaseInfo = super.moduleBaseInfo();
        moduleBaseInfo.setAuto(true);
        moduleBaseInfo.setAsyn(false);
        moduleBaseInfo.setReverse(true);
        return moduleBaseInfo;
    }
}
